package com.mofeng.myshop.controller.user;

import com.mofeng.myshop.domain.dto.SendEmailDto;
import com.mofeng.myshop.service.user.QrCodeService;
import com.mofeng.myshop.service.user.UserUtilsService;
import com.mofeng.myshop.until.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user/utils")
@RequiredArgsConstructor
@Tag(name = "用户相关的工具接口", description = "用户相关的工具接口")
public class UserUtilsController {

    private final UserUtilsService userUtilsService;

    private final QrCodeService qrCodeService;


    @Operation(summary = "判断用户名是否存在")
    @GetMapping("/check/user_name/{user_name}")
    public R hasUserName(@PathVariable("user_name") String userName) {
        if (userUtilsService.checkUserName(userName)) {
            return R.FAILED("此用户名已存在");
        }
        return R.SUCCESS("此用户名不存在");
    }

    @Operation(summary = "判断邮箱是否被注册")
    @GetMapping("/check/email/{email}")
    public R hasEmail(@PathVariable("email") String email) {
        if (userUtilsService.checkEmail(email)) {
            return R.FAILED("邮箱已注册！");
        }
        return R.SUCCESS("邮箱未注册");
    }


    @Operation(summary = "判断图灵验证码是否有效")
    @GetMapping("/check/captcha_code_right/{captcha_code}")
    public R isCaptchaCodeCodeRight(@PathVariable("captcha_code") String captchaCode) {
        return userUtilsService.isCaptchaCodeRight(captchaCode);
    }

    @Operation(summary = "用户修改密码/邮箱验证邮箱")
    @GetMapping("/check/email_code")
    public R checkEmailCode(@RequestParam("email") String email,
                            @RequestParam("emailCode") String emailCode) {
        return userUtilsService.checkEmailCode(email, emailCode);
    }

    @Operation(summary = "检查用户是否验证过图灵码")
    @GetMapping("/check/captcha_code")
    public R checkCaptchaCode() {
        try {
            userUtilsService.checkCaptchaState();
        } catch (Exception e) {
            e.printStackTrace();
            return R.FAILED();
        }
        return R.SUCCESS();
    }

//    @Operation(summary = "用户提交Bug")
//    @PostMapping("/bug")
//    public R subBug(@RequestBody EmailMessage message) {
//        return userUtilsService.subBug(message);
//    }

//    @Operation(summary = "给某人发送邮件")
//    @PostMapping("/send-one")
//    public R sendMessageToOne(@RequestParam("id") String id,
//                                           @RequestParam("message") String message,
//                                           @RequestParam(value = "title", required = false) String title) {
//        return userUtilsService.sendMessageToOne(id, message, title);
//    }

//    @Operation(summary = "群发邮件")
//    @PostMapping("/send-all")
//    public R sendMessageToAll(@RequestParam("message") String message,
//                                           @RequestParam(value = "title", required = false) String title) {
//        return userUtilsService.sendMessageToAll(message, title);
//    }


    @Operation(summary = "获取图灵验证码(有效时间5分钟)")
    @GetMapping("/captcha_code")
    public void getVerificationCode() {
        userUtilsService.getCaptchaCode();
    }


    @Operation(summary = "发送邮件（发送验证码）  注册register、找回密码forget、修改邮箱update(会输入新的邮箱)")
    @GetMapping("/email")
    public R sendVerificationCodeByEmail(@RequestParam("type") String type,
                                         @RequestParam("email") String emailAddress) {
        return userUtilsService.sendEmailCode(type, emailAddress);
    }

    @Operation(summary = "获取二维码: 二维码的图片路径 二维码的内容字符串")
    @GetMapping("/pc_logi_qr")
    public R getPcLoginQrCode() {
        return qrCodeService.getPcLoginQrCodeInfo();
    }

    @Operation(summary = "检查二维码的状态")
    @GetMapping("/qr-code-state/{loginId}")
    public R checkQrCodeLoginState(@PathVariable("loginId") String loginId) {
        return qrCodeService.checkQrCodeLoginState(loginId);
    }

    @Operation(summary = "更新二维码的登录状态")
    @PutMapping("/qr-code-state/{loginId}")
    public R updateQrCodeLoginState(@PathVariable("loginId") String loginId) {
        return qrCodeService.updateQrCodeLoginState(loginId);
    }

}
